import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

import java.io.*;
import java.util.*;

/**
 * @author Administrator
 * @version 2021/7/1 0001
 */
public class BatchCreateDirTool {

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入根目录：");
        File fileDir = new File(scanner.nextLine());

        File file = new File(fileDir + File.separator + "index.ly");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String path;
        List<String> prefix = CollectionUtil.newArrayList("", "", "", "", "", "", "", "", "", "");
        while ((path = bufferedReader.readLine()) != null) {
            final int length = path.length();
            final int strimLength = path.replaceAll(" {4}", "").length();
            int index = (length - strimLength) / 4;
            prefix.set(index, path.replaceAll(" {4}", "").replaceAll(" ", "_"));

            if (!path.startsWith("    ")) {
                // 一级目录，直接创建目录
                createDir(fileDir, path.replaceAll(" ","_"));
            } else {
                // 非一级目录需要带前缀
                StringJoiner stringJoiner = new StringJoiner(File.separator);
                CollectionUtil.filter(prefix, StrUtil::isNotEmpty).forEach(stringJoiner::add);
                createDir(fileDir, stringJoiner.toString());
            }
        }
    }

    private static void createDir(File fileDir, String path) {
        File fileTemp = new File(fileDir.getPath() + File.separator + path);
        if (!fileTemp.exists()) {
            boolean mkdir = fileTemp.mkdir();
            if (!mkdir) {
                System.out.println("目录[" + fileTemp.getPath() + "]创建失败");
            }
        }
    }
}
